home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PROGEDIT / 0748.ZIP / WP < prev    next >
Text File  |  1986-12-29  |  2KB  |  146 lines

  1. init()
  2. {
  3.   assign_key("just_para",  10);   /* <CTRL> J */
  4.   assign_key("reform",     11);   /* <CTRL> K */
  5. }
  6.  
  7. reform()
  8. {
  9.   int len;
  10.   int margin;
  11.   int oldlineno;
  12.   string buf;
  13.  
  14.   buf = get_tty_str("What is the right margin? ");
  15.   if ((margin = atoi(buf)) <= 0) return;
  16.   oldlineno = currlinenum();
  17.  
  18.   while (!is_line_blank(currline()))
  19.   {
  20.     if ((len = strlen(currline())) == 0)
  21.       break;
  22.     if (len > margin)
  23.     {
  24.       setcol(margin);
  25.       if (currchar() != ' ')
  26.         prevword();
  27.       insert("\n");
  28.       while (!is_eol() && currchar() == ' ')  delchar();
  29.     }
  30.     else if (len <= margin)
  31.     {
  32.       if (down() && is_line_blank(currline())) break;
  33.       up();
  34.       goeol();
  35.       left();
  36.       while (currchar() == ' ' && currcol() > 1)
  37.       {
  38.         delchar();   left();
  39.       }
  40.       goeol();
  41.       insert(" ");
  42.       delchar();
  43.     }
  44.   }
  45.   goline(oldlineno);
  46.   gobol();
  47. }
  48.  
  49.  
  50.  
  51. just_para()
  52. {
  53.   while (!is_line_blank(currline()))
  54.   {
  55.     expand();
  56.     down();
  57.   }
  58. }
  59.  
  60.  
  61. expand()
  62. {
  63.   string text, new;
  64.   int    margin, nextra, nholes, nb;
  65.  
  66.   margin = 65;
  67.  
  68.   squash();
  69.   text   = currline();
  70.   nholes = wordcount(text) - 1;
  71.   nextra = margin - strlen(text);
  72.  
  73.   new = "";
  74.   gobol();
  75.  
  76.   while (!is_eol() && nholes > 0)
  77.   {
  78.     c = currchar();
  79.     new = strcat(new, chr(c));
  80.     if (c == ' ')
  81.     {
  82.       new = strcat(new, repstr(" ", nb = nextra/nholes));
  83.       nextra = nextra - nb;
  84.       nholes = nholes - 1;
  85.     }
  86.     right();
  87.   }
  88.  
  89.  
  90.   while (!is_eol())
  91.   {
  92.     c = currchar();
  93.     new = strcat(new, chr(c));
  94.     right();
  95.   }
  96.  
  97.   gobol();
  98.   deleol();
  99.   insert(new);
  100.   gobol();
  101. }
  102.  
  103. squash()
  104. {
  105.   gobol();
  106.   while (!is_eol())
  107.   {
  108.     if (currchar() == ' ')
  109.     {
  110.       right();
  111.       while (!is_eol() && currchar() == ' ')  delchar();
  112.     }
  113.     else
  114.       right();
  115.   }
  116.   gobol();
  117. }
  118.  
  119.  
  120. wordcount(str)
  121.   string str;
  122. {
  123.   int count, i, len;
  124.  
  125.   count = 1;
  126.   len = strlen(str);
  127.  
  128.   for (i = 1;  i <= len;  i = i + 1)
  129.     if (substr(str, i, 1) == " ")  count = count + 1;
  130.   return count;
  131. }
  132.  
  133. is_line_blank(str)
  134.   string str;
  135. {
  136.   int i;
  137.   int len;
  138.  
  139.   len = strlen(str);
  140.  
  141.   for (i = 1;  i <= len;  i = i + 1)
  142.     if (substr(str, i, 1) != " ")
  143.       return 0;
  144.   return 1;
  145. }
  146.